import { defineStore } from 'pinia';
import { ref } from 'vue';

export const useUserStore = defineStore('user', () => {
    const token = ref(localStorage.getItem('token') || '');
    const username = ref('');
    const userType = ref(0);

    const setToken = (newToken) => {
        token.value = newToken;
        localStorage.setItem('token', newToken);
    };

    const setUserInfo = (info) => {
        username.value = info.username;
        userType.value = info.userType;
    };
    const logout = () => {
        token.value = '';
        username.value = '';
        userType.value = 0;
        localStorage.removeItem('token');
    };

    return { token, username, userType, setToken, setUserInfo,logout };
});
